
package com.insigma.business.qrcode.soldier.service.impl;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;

import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.util.AdminAreaCodeUtils;
import com.insigma.business.qrcode.common.util.DateUtils;
import com.insigma.business.qrcode.soldier.dao.SoldierUnemploymentRegistrationInfoDao;
import com.insigma.business.qrcode.soldier.dto.SequenceNameConstant;
import com.insigma.business.qrcode.soldier.dto.UnemploymentSourceTypeEnum;
import com.insigma.business.qrcode.soldier.entity.DataStatsCDO;
import com.insigma.business.qrcode.soldier.entity.SoldierUnemploymentRegistrationInfoDO;
import com.insigma.business.qrcode.soldier.request.SoldierUnemploymentRegistrationInfoBSearcher;
import com.insigma.business.qrcode.soldier.service.SoldierUnemploymentRegistrationInfoService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service("soldierUnemploymentRegistrationInfoService")
public class SoldierUnemploymentRegistrationInfoServiceImpl implements SoldierUnemploymentRegistrationInfoService {

    @Autowired(required = false)
    private SoldierUnemploymentRegistrationInfoDao soldierUnemploymentRegistrationInfoDao;
    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;


    private List<String> areaList = Lists.newArrayList("上城区", "临安区", "临平区", "余杭区", "富阳区", "建德市", "拱墅区", "桐庐县", "淳安县", "滨江区", "萧山区", "西湖区", "钱塘区", "风景区");


    @Override
    public List<SoldierUnemploymentRegistrationInfoDO> search(final SoldierUnemploymentRegistrationInfoBSearcher soldierUnemploymentRegistrationInfoBSearcher) {
        if (Strings.isNotBlank(soldierUnemploymentRegistrationInfoBSearcher.getAdminAreaCode())) {
            soldierUnemploymentRegistrationInfoBSearcher.setAdminAreaCode(AdminAreaCodeUtils.getCutdAreaCode(soldierUnemploymentRegistrationInfoBSearcher.getAdminAreaCode()));
        }
        return soldierUnemploymentRegistrationInfoDao.search(soldierUnemploymentRegistrationInfoBSearcher);
    }

    @Override
    public SoldierUnemploymentRegistrationInfoDO get(final String id) {
        return soldierUnemploymentRegistrationInfoDao.get(id);
    }


    @Override
    public Integer insert(final SoldierUnemploymentRegistrationInfoDO soldierUnemploymentRegistrationInfoDO) {
        soldierUnemploymentRegistrationInfoDO.setId(sequenceGenerator.genKey(SequenceNameConstant.SOLDIER_UNEMPLOYMENT_REGISTRATION_INFO_ID));
        if (soldierUnemploymentRegistrationInfoDO.getVisited() == null) {
            soldierUnemploymentRegistrationInfoDO.setVisited(false);
        }
        if (Strings.isBlank(soldierUnemploymentRegistrationInfoDO.getInitStatus())) {
            soldierUnemploymentRegistrationInfoDO.setInitStatus(soldierUnemploymentRegistrationInfoDO.getType());
        }

        if (Strings.isBlank(soldierUnemploymentRegistrationInfoDO.getSource())) {
            soldierUnemploymentRegistrationInfoDO.setSource(UnemploymentSourceTypeEnum.MANUALLY.name());
        }
        return soldierUnemploymentRegistrationInfoDao.insert(soldierUnemploymentRegistrationInfoDO);
    }


    @Override
    public Integer update(final SoldierUnemploymentRegistrationInfoDO soldierUnemploymentRegistrationInfoDO) {
        return soldierUnemploymentRegistrationInfoDao.update(soldierUnemploymentRegistrationInfoDO);
    }


    @Override
    public Integer delete(final String id, final String modifierType, final String modifierId) {
        return soldierUnemploymentRegistrationInfoDao.delete(id, modifierType, modifierId);
    }

    @Override
    public List<SoldierUnemploymentRegistrationInfoDO> getByIdCardNo(String idCardNo) {
        return soldierUnemploymentRegistrationInfoDao.getByIdCardNo(idCardNo);
    }

    @Override
    public List<SoldierUnemploymentRegistrationInfoDO> getByIdCardNoWithDeleted(String idCardNo) {
        return soldierUnemploymentRegistrationInfoDao.getByIdCardNoWithDeleted(idCardNo);
    }

    @Override
    public List<SoldierUnemploymentRegistrationInfoDO> listAllUnemploymentSolider(SoldierUnemploymentRegistrationInfoBSearcher searcher) {
        return soldierUnemploymentRegistrationInfoDao.listAllUnemploymentSolider(searcher);
    }

    @Override
    public Integer countByType(String type) {
        return soldierUnemploymentRegistrationInfoDao.countByType(type);
    }

    @Override
    public Integer countByTypeAndArea(String type, String adminAreaCode) {
        Integer integer = soldierUnemploymentRegistrationInfoDao.countByTypeAndArea(type, AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
        if (integer == null) {
            integer = 0;
        }
        return integer;
    }

    @Override
    public Integer countByStatus(String status) {
        return soldierUnemploymentRegistrationInfoDao.countByStatus(status);
    }

    @Override
    public Integer countByStatusAndArea(String status, String adminAreaCode) {
        Integer integer = soldierUnemploymentRegistrationInfoDao.countByStatusAndArea(status, AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
        if (integer == null) {
            integer = 0;
        }
        return integer;
    }


    @Override
    public Integer countNotEmployment(String adminAreaCode) {
        return soldierUnemploymentRegistrationInfoDao.countNotEmployment(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%");
    }

    @Override
    public List<DataStatsCDO> countUnemploymentRegistrationAreaMap(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);

        return soldierUnemploymentRegistrationInfoDao.countUnemploymentRegistrationAreaMap(areaLike, nextLevel, levelLength);
    }

    @Override
    public List<DataStatsCDO> countUnemploymentRegistrationAreaMap(String adminAreaCode, List<String> statusList) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);

        return soldierUnemploymentRegistrationInfoDao.countUnemploymentRegistrationAreaMapByStatus(areaLike, nextLevel, levelLength, statusList);
    }

    @Override
    public List<DataStatsCDO> countUnemploymentRegistrationAgeMap(String adminAreaCode) {
        return soldierUnemploymentRegistrationInfoDao.countUnemploymentRegistrationAgeMap(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%");
    }

    @Override
    public Integer countNotEmploymentByStatusList(String adminAreaCode, List<String> status) {
        return soldierUnemploymentRegistrationInfoDao.countNotEmploymentByStatusList(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%", status, DateUtil.format(DateUtils.getLastDayOfPreviousMonth(), "yyyy-MM-dd"));
    }

    @Override
    public List<SoldierUnemploymentRegistrationInfoDO> listAllAutoEmployment() {
        return soldierUnemploymentRegistrationInfoDao.listAllAutoEmployment();
    }

    @Override
    public List<DataStatsCDO> groupTypeAreaMap(String adminAreaCode, String source) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);

        return soldierUnemploymentRegistrationInfoDao.groupTypeAreaMap(areaLike, nextLevel, levelLength, source);
    }

    @Override
    public Integer countBySource(String adminAreaCode, String source, Date startDate) {
        return soldierUnemploymentRegistrationInfoDao.countBySource(adminAreaCode, source, DateUtil.format(startDate, "yyyy-MM-dd"));
    }

    @Override
    public Integer countByCreateDatetime(String adminAreaCode, String month) {
        return soldierUnemploymentRegistrationInfoDao.countByCreateDatetime(adminAreaCode, month);
    }

    @Override
    public List<DataStatsCDO> groupAreaByTime(String adminAreaCode, Date startDate, Date endDate) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);

        List<DataStatsCDO> dataStatsCDOS = soldierUnemploymentRegistrationInfoDao.groupAreaByTime(areaLike, nextLevel, levelLength, startDate, endDate);
        if (level == 2) {
            for (String area : areaList) {
                boolean flag = false;
                for (DataStatsCDO a : dataStatsCDOS) {
                    if (area.equals(a)) {
                        flag = true;
                    }
                }
                if (!flag) {
                    dataStatsCDOS.add(new DataStatsCDO(area, area, 0));
                }
            }
        }

        return dataStatsCDOS;
    }

    @Override
    public List<Double> listVisitSecondDiff(String adminAreaCode) {
        return soldierUnemploymentRegistrationInfoDao.listVisitSecondDiff(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
    }

    @Override
    public List<DataStatsCDO> groupAreaByTimeAndIsVisited(String adminAreaCode, Date startDate, Date endDate, boolean visited) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);

        return soldierUnemploymentRegistrationInfoDao.groupAreaByTimeAndIsVisited(areaLike, nextLevel, levelLength, startDate, endDate, visited);
    }

    @Override
    public List<DataStatsCDO> groupHistory(String adminAreaCode, List<String> registrationStatusList, List<String> historyStatusList) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);

        return soldierUnemploymentRegistrationInfoDao.groupHistory(areaLike, nextLevel, levelLength, registrationStatusList, historyStatusList);
    }

    @Override
    public Integer countHistory(String adminAreaCode, String historyStatus) {
        return soldierUnemploymentRegistrationInfoDao.countHistory(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode), historyStatus);
    }

    @Override
    public Integer countRegistrationHistory(String adminAreaCode, ArrayList<String> registrationStatusList, ArrayList<String> historyStatusList) {
        return soldierUnemploymentRegistrationInfoDao.countRegistrationHistory(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode), registrationStatusList, historyStatusList);
    }


}
